package org.ilead.iserve.web;

import java.util.Date;
import java.util.List;

import org.apache.log4j.Logger;
import org.ilead.iserve.model.HelpInformationMessage;
import org.ilead.iserve.model.PrivateMessage;
import org.ilead.iserve.model.User;
import org.ilead.iserve.service.IHelpInformationMessageService;
import org.ilead.iserve.service.IPrivateMessageService;
import org.ilead.iserve.service.IUserService;
import org.ilead.iserve.service.impl.HelpInformationMessageService;
import org.ilead.iserve.service.manager.UserManager;
import org.ilead.iserve.web.base.BaseActionSupport;

public class HelpMessageAction extends BaseActionSupport {

	Logger logger = Logger.getLogger(HelpMessageAction.class);
	/**
	 * 
	 */
	private Long topicId;
	private String title;
	private String userId;
	
	
	private Long id;
	private static final long serialVersionUID = 1L;
	private IHelpInformationMessageService helpInformationMessageService;
	private IUserService userService;
	private IPrivateMessageService privateMessageService;
	private HelpInformationMessage helpInformationMessage;
	public String saveHelpMessage() throws Exception{
		User user = UserManager.getInstance().getLoginUser();
		helpInformationMessage = new HelpInformationMessage();
		helpInformationMessage.setTopicId(topicId);
		helpInformationMessage.setTitle(title);
		helpInformationMessage.setReplierId(user.getLoginName());
		helpInformationMessage.setReplierName(user.getUserName());
		helpInformationMessage.setCreateTime(new Date());
		helpInformationMessage.setStatus("1");
		helpInformationMessageService.saveAll(helpInformationMessage);
		//发送站内信
		List<User> list = userService.findByLoginName(userId);
		if(list!=null&&list.size()>0){
			User targetUser = list.get(0);
			PrivateMessage privateMessage = new PrivateMessage();
			privateMessage.setContent("<a href='detailHelpInformation.action?id="+topicId+"'>点击链接查看留言</a>");
			privateMessage.setTitle("您发布的帖子有来自"+user.getUserName()+"的留言");
			privateMessage.setSendTime(new Date());
			privateMessage.setSource(user.getId());
			privateMessage.setTarget(targetUser.getId());
			privateMessage.setReState(0);
			privateMessage.setSeState(0);
			privateMessageService.addMessage(privateMessage);
		}
		return SUCCESS;
	}
	public String saveSubHelpMessage() throws Exception{
		User user = UserManager.getInstance().getLoginUser();
		helpInformationMessage = new HelpInformationMessage();
		HelpInformationMessage helpInformationMessagePar = helpInformationMessageService.findById(id);
		helpInformationMessage.setPid(helpInformationMessagePar.getId());
		helpInformationMessage.setReplierId(user.getLoginName());
		helpInformationMessage.setReplierName(user.getUserName());
		helpInformationMessage.setRid(helpInformationMessagePar.getRid());
		helpInformationMessage.setStatus("1");
		helpInformationMessage.setTitle(title);
		helpInformationMessage.setTopicId(helpInformationMessagePar.getTopicId());
		helpInformationMessage.setCreateTime(new Date());
		helpInformationMessageService.save(helpInformationMessage);
		topicId = helpInformationMessagePar.getTopicId();
		
		
		
		return SUCCESS;
	}
	public Long getTopicId() {
		return topicId;
	}
	public void setTopicId(Long topicId) {
		this.topicId = topicId;
	}
	public String getTitle() {
		return title;
	}
	public void setTitle(String title) {
		this.title = title;
	}

/*	public String getReplierId() {
		return replierId;
	}
	public void setReplierId(String replierId) {
		this.replierId = replierId;
	}*/
	public void setHelpInformationMessageService(
			IHelpInformationMessageService helpInformationMessageService) {
		this.helpInformationMessageService = helpInformationMessageService;
	}
	public Long getId() {
		return id;
	}
	public void setId(Long id) {
		this.id = id;
	}
	public void setUserService(IUserService userService) {
		this.userService = userService;
	}
	public void setPrivateMessageService(
			IPrivateMessageService privateMessageService) {
		this.privateMessageService = privateMessageService;
	}
	public String getUserId() {
		return userId;
	}
	public void setUserId(String userId) {
		this.userId = userId;
	}
	
	
}
